﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Forms;
using System.Windows.Media.Animation;

namespace TeenyGrab.Outputs
{
    /// <summary>
    /// Interaction logic for FtpOutputWindow.xaml
    /// </summary>
    public partial class UrlOutputWindow : Window
    {
        private Timer T { get; set; }
        private int TimeLeft { get; set; } // in seconds

        private static int WindowCount = 0;

        public UrlOutputWindow(string url)
        {
            InitializeComponent();
            textBoxUrl.Text = url;
            textBoxUrl.SelectAll();

            WindowCount++;

            var screen = Screen.PrimaryScreen.Bounds;
            Left = screen.X + screen.Width - this.Width;
            Top = screen.Y + screen.Height - (this.Height * WindowCount);

            TimeLeft = 5;

            T = new Timer();
            T.Interval = 1000;
            T.Tick += new EventHandler((o, e) =>
            {
                if (TimeLeft == 0)
                {
                    T.Stop();
                    this.Close();
                }
                TimeLeft--;
            });
            T.Start();
        }

        private void textBoxUrl_MouseDown(object sender, MouseButtonEventArgs e)
        {

        }

        private void textBoxUrl_SelectionChanged(object sender, RoutedEventArgs e)
        {
            if (textBoxUrl.SelectedText != textBoxUrl.Text)
            {
                textBoxUrl.SelectAll();
            }
        }

        private void buttonCopy_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Clipboard.SetText(textBoxUrl.Text);
        }

        private void buttonClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            Closing -= Window_Closing;
            e.Cancel = true;
            var anim = new DoubleAnimation(0, (Duration)TimeSpan.FromSeconds(1));
            anim.Completed += (s, _) => this.Close();
            this.BeginAnimation(UIElement.OpacityProperty, anim);
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            WindowCount--;
        }
    }
}
